/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assign2;

import assign2.ListGUI.LoadFileListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.JFileChooser;

/**
 *
 * @author martyastiadi
 */
public class ListGUI implements Runnable {

    private ArrayList<Product> product = new ArrayList<Product>();
    private ArrayList<Inventory> inventory = new ArrayList<Inventory>();
//    private ArrayList<Inventory> itemList = new ArrayList<Inventory>();
//    private ArrayList<Integer> qtyList = new ArrayList<Integer>();
    JTextArea text = new JTextArea(10, 10);
    private DefaultListModel model = new DefaultListModel();
    private DefaultListModel model2 = new DefaultListModel();
    private File file = new File("Inventory.xml");
    JFrame frame;
    JList list = new JList(model);
    JScrollPane listScroller = new JScrollPane(list);
    


    public void run() {
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setLayoutOrientation(JList.VERTICAL_WRAP);
        list.setVisibleRowCount(5);
        listScroller.setPreferredSize(new Dimension(250, 80));

        frame = new JFrame("SCM - Yusup, port @ (     ) ");
        //JTextField field = new JTextField();


        text.setText("Load XML file first!!!");
        text.setEditable(false);

        JMenuBar menu1 = new JMenuBar();
        JMenu m_product = new JMenu("Product");
        menu1.add(m_product);
        JMenuItem m_product1 = new JMenuItem("List required items");
        m_product.add(m_product1);
        m_product1.addActionListener(new ListProduct());
        JMenuItem m_product2 = new JMenuItem("Make Order");
        m_product.add(m_product2);

        JMenu m_print = new JMenu("Print");
        JMenuItem m_print1 = new JMenuItem("Product List");
        m_print.add(m_print1);
        m_print1.addActionListener(new PrintProduct());
        JMenuItem m_print2 = new JMenuItem("Inventory List");
        m_print.add(m_print2);
        m_print2.addActionListener(new PrintItem());
        menu1.add(m_print);

        JMenu m_file = new JMenu("File");
        JMenuItem m_file1 = new JMenuItem("Load File");
        m_file.add(m_file1);
        m_file1.addActionListener(new LoadFileListener());
        JMenuItem m_file2 = new JMenuItem("Save File");
        m_file.add(m_file2);
        m_file2.addActionListener(new SaveFileListener());
        menu1.add(m_file);

        JMenu m_add = new JMenu("Add");
        JMenuItem m_add1 = new JMenuItem("Product");
        m_add.add(m_add1);
        m_add1.addActionListener(new AddProd());
        JMenuItem m_add2 = new JMenuItem("Inventory");
        m_add.add(m_add2);
        m_add2.addActionListener(new AddInven());
        menu1.add(m_add);

        JMenu m_mess = new JMenu("Message");
        JMenuItem m_mess1 = new JMenuItem("Send Message");
        m_mess.add(m_mess1);
        m_mess1.addActionListener(new Send());
        JMenuItem m_mess2 = new JMenuItem("View Message");
        m_mess.add(m_mess2);
        menu1.add(m_mess);

        frame.setJMenuBar(menu1);
//        frame.add(field);
        Container content = frame.getContentPane();
        frame.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
        content.add(text);
        content.add(list);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.getContentPane().add(button);
        frame.setSize(300, 400);
        frame.setVisible(true);
        frame.repaint();

    }

    class LoadFileListener implements ActionListener {
    
        public void actionPerformed(ActionEvent f) {
            JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();}

                try {

                    XMLDecoder dec = new XMLDecoder(new BufferedInputStream(new FileInputStream(file)));
                    inventory = (ArrayList<Inventory>) dec.readObject();
                    //dec.readObject();
                    for (Inventory item : inventory) {
                        if (item instanceof Product) {
                        Product prod = (Product) item;
//                        System.out.println(prod.getName());
                        product.add(prod);
                    }
                }
            } catch (IOException e) {
                //System.out.println ("Testing");
                System.exit(1);
            }
//            list.setVisible(false);
        }
    }

    class SaveFileListener implements ActionListener {

        public void actionPerformed(ActionEvent s) {
            try {
                XMLEncoder enc = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(file)));
                // inventory = (ArrayList<Inventory>)
                enc.writeObject(inventory);
                enc.close();
            } catch (IOException e) {
                System.exit(1);
            }
//            list.setVisible(false);
        }
    }

    class PrintProduct implements ActionListener {

        public void actionPerformed(ActionEvent pr) {
            String prodList = new String();
            for (Inventory item : inventory) {
                if (item instanceof Product) {
                    Product prod = (Product) item;
//                    System.out.println(prod.getName());
                    prodList = prodList + prod.getName() + "\n";
                    product.add(prod);
                }
            }
            text.setText(prodList);
            list.setVisible(false);
        }
    }

    class PrintItem implements ActionListener {

        public void actionPerformed(ActionEvent pr) {
            String itList = new String();
            for (int i = 0; i < inventory.size(); ++i) {
                itList = itList + inventory.get(i).getName() + " : " + inventory.get(i).getQty() + " : " + inventory.get(i).getResQty() + "\n";
            }
            text.setText(itList);
            list.setVisible(false);
        }
    }

    class ListProduct implements ActionListener {

        private String list2;

        public void actionPerformed(ActionEvent arg0) {

            String prodList = new String();
            int i = 0;
            for (Inventory item : inventory) {
                if (item instanceof Product) {
                    Product prod = (Product) item;
                    prodList = prodList + prod.getName() + "\n";
                    model.add(i++, prod.getName());
                }
            }

            MouseListener mouseListener = new MouseAdapter() {

                int index = 0;

                public void mouseClicked(MouseEvent mouseEvent) {
                    JList theList = (JList) mouseEvent.getSource();
                    if (mouseEvent.getClickCount() == 2) {
                        int index = theList.locationToIndex(mouseEvent.getPoint());
                        if (index >= 0) {

//                            Perlu diperbaiki...

                            ArrayList<Inventory> tmp = product.get(0).getItemList();
                            for (int i = 0; i < tmp.size(); ++i) {

                                text.append(tmp.get(i).getName() + " | " + tmp.get(i).getQty() + "\n");
                            }
                        }
                    } 
                }
            }; 

            list.addMouseListener(mouseListener);
            list.setVisible(true);
            text.setText(list2);
//           text.setVisible(true);
        }
    }

    class AddProd implements ActionListener {

        JFrame frame;
        JFrame frame2;
        JTextField textField = new JTextField(("Product"));
        JTextField textField2 = new JTextField("0");
        JTextField textField3 = new JTextField("0");
        JList list3 = new JList(model2);
        JScrollPane listScroller2 = new JScrollPane(list3);
        

        public void actionPerformed(ActionEvent a) {
            list3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list3.setLayoutOrientation(JList.VERTICAL_WRAP);
            list3.setVisibleRowCount(15);
            listScroller2.setPreferredSize(new Dimension(200, 200));

            frame = new JFrame("Inventory");
            BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS); // top to bottom
            frame.setLayout(boxLayout);
            frame.add(new JLabel("Product: "));
            frame.add(textField);
            frame.add(new JLabel("Quantity: "));
            frame.add(textField2);
            frame.add(new JLabel("Reserve: "));
            frame.add(textField3);
            JButton button = new JButton("Get Inventory");

            button.addActionListener(new ActionListener() {

                private String list4;
                JTextArea text2 = new JTextArea();
                public void actionPerformed(ActionEvent actionEvent) {
                    frame2 = new JFrame("Inventory List");
                    String itList = new String();
                    for (int i = 0; i < inventory.size(); ++i) {
                        itList = itList + inventory.get(i).getName() + " : " + inventory.get(i).getQty() + " : " + inventory.get(i).getResQty() + "\n";
//                        model2.add(i++, inventory.get(i).getName());
                    }
                    text2.setText(itList);
                    text2.setVisible(true);
//                    String prodList = new String();
//                    int i = 0;
                    
//                    for (Inventory item : inventory) {
//                        if (item instanceof Product) {
//                            Product prod = (Product) item;
//                            prodList = prodList + prod.getName() + "\n";
//                            model2.add(i++, prod.getName());
//                        }
//                    }

                    MouseListener mouseListener2 = new MouseAdapter() {

                        int index = 0;

                        public void mouseClicked(MouseEvent mouseEvent) {
                            JList theList = (JList) mouseEvent.getSource();
                            if (mouseEvent.getClickCount() == 2) {
                                int index = theList.locationToIndex(mouseEvent.getPoint());
                                if (index >= 0) {

//                            Perlu diperbaiki...

                                    ArrayList<Inventory> tmp = product.get(0).getItemList();
                                    for (int i = 0; i < tmp.size(); ++i) {
//                                list2 = list2 + tmp.get(i).getName() + "";
//                                text.append(list2);
                                        text2.append(tmp.get(i).getName() + " | " + tmp.get(i).getQty() + "\n");
                                    }
                                }
                            }
                        }
                    };
                    Container content = frame2.getContentPane();
                    frame2.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
                    content.add(text2);
                    content.add(list3);
                    list3.addMouseListener(mouseListener2);
                    list3.setVisible(true);
                    frame2.setSize(200, 300);
//                    text2.setText(list4);
//                    text2.setVisible(true);

                    frame2.setVisible(true);
                }
            });

            text.setText(textField.getText() + " : " + textField2.getText() + " : " + textField3.getText());




            frame.add(button);

            frame.pack();

            frame.setSize(250, 200);
//            list.setVisible(false);
            frame.setVisible(true);
        }
    }

    class AddInven implements ActionListener {

        JFrame frame;
        JTextField textField = new JTextField(("Product"));
        JTextField textField2 = new JTextField("0");
        JTextField textField3 = new JTextField("0");

        public void actionPerformed(ActionEvent a) {

            frame = new JFrame("Inventory");
            BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS); // top to bottom
            frame.setLayout(boxLayout);
            frame.add(new JLabel("Product: "));
            String txt = textField.getText();
            frame.add(textField);
            frame.add(new JLabel("Quantity: "));
            String txt2 = textField2.getText();
            frame.add(textField2);
            frame.add(new JLabel("Reserve: "));
            String txt3 = textField3.getText();
            frame.add(textField3);

            JButton button = new JButton("Add Inventory");
            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent actionEvent) {

                    text.setText(textField.getText() + " : " + textField2.getText() + " : " + textField3.getText());
                }
            });

            frame.add(button);

            frame.pack();

            frame.setSize(250, 200);
            list.setVisible(false);
            frame.setVisible(true);


        }
    }

    class Send implements ActionListener {

        public void actionPerformed(ActionEvent arg0) {
            JFrame frame = new JFrame();
            Object result = JOptionPane.showInputDialog(frame, "Remote Address:");
            text.setText((String) result);
                    text.setVisible(true);
//            System.out.println(result);
        }
    }
}


